package com.server.dataSource;

import com.dao.chart.ChartsConfigMapper;
import com.dao.chart.ShowChartsMapper;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.vo.configVo.ColumnScreenInfo;
import com.server.chart.GetChartsDataUtilService;
import com.server.chart.ChartsUtilService;
import com.config.BasesSource.DynamicDataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ColumnScreenService {
    @Autowired
    ShowChartsMapper showChartsMapper;
    @Autowired
    GetChartsDataUtilService getChartsDataUtilService;
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    ChartsUtilService chartsUtilService;

    public RepEntity getScreenData(ColumnScreenInfo columnScreenInfo, String token){
        //取表名
        int id = columnScreenInfo.getId();
        String tableName = chartsUtilService.getSqlStr(token, id, 0);
        if ("".equals(tableName)){
            return new RepEntity(RepCode.NoAuthority);
        }

        List<Object> data = new ArrayList<>();

        int baseId = getChartsDataUtilService.getBaseId(id);
        System.out.println("切换数据库"+String.valueOf(baseId));

        try{
            if (!DynamicDataSourceContextHolder.isContainsDataSource(String.valueOf(baseId))) {
                //joinPoint.getSignature() ：获取连接点的方法签名对象
                System.out.println("数据源 " + String.valueOf(baseId) + " 不存在使用默认的数据源 -> ");
                return new RepEntity(RepCode.DataSourceNull);
            } else {
                System.out.println("使用数据源：" + String.valueOf(baseId));
                DynamicDataSourceContextHolder.setDataSourceType(String.valueOf(baseId));
            }
            data =  showChartsMapper.getScreenData(columnScreenInfo.getColumnName(), tableName);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("异常");
            return new RepEntity(RepCode.erro);
        }finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return new RepEntity(RepCode.success, data);
    }

    /*
    通过数据源ID获取列的值
     */
    public RepEntity getScreenByBaseId(ColumnScreenInfo columnScreenInfo, String token){
        //取表名
        int connectId = columnScreenInfo.getId();
        String tableName = chartsConfigMapper.getTableNameByBase(connectId);
        if (tableName == null || "".equals(tableName)){
            return new RepEntity(RepCode.nullTableName);
        }else {
            tableName = "(" + tableName +")";
        }

        List<Object> data = new ArrayList<>();

        int baseId =  getChartsDataUtilService.getBaseIdByConnect(connectId);
        System.out.println("切换数据库"+String.valueOf(baseId));

        try{
            if (!DynamicDataSourceContextHolder.isContainsDataSource(String.valueOf(baseId))) {
                //joinPoint.getSignature() ：获取连接点的方法签名对象
                System.out.println("数据源 " + String.valueOf(baseId) + " 不存在使用默认的数据源 -> ");
                return new RepEntity(RepCode.DataSourceNull);
            } else {
                System.out.println("使用数据源：" + String.valueOf(baseId));
                DynamicDataSourceContextHolder.setDataSourceType(String.valueOf(baseId));
            }
            data =  showChartsMapper.getScreenData(columnScreenInfo.getColumnName(), tableName);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("异常");
            return new RepEntity(RepCode.erro);
        }finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return new RepEntity(RepCode.success, data);
    }
}
